package my.jbf.firstGame.SURFACEVIEW;
import android.content.*;
import android.view.*;

import java.util.*;

import android.graphics.*;

import android.util.*;
import android.app.*;

import my.jbf.firstGame.BITMAP.AttackButton;
import my.jbf.firstGame.BITMAP.FightMap;
import my.jbf.firstGame.BITMAP.SmallMap;
import my.jbf.firstGame.GAMEDATA.GameDBManager;
import my.jbf.firstGame.GAMEDATA.SharedPreferenceManager;


import my.jbf.firstGame.MODULE.MyParsePath;
import my.jbf.firstGame.MainActivity;
import my.jbf.firstGame.PLAYER.Bullet;
import my.jbf.firstGame.PLAYER.Player;
import my.jbf.firstGame.BITMAP.*;
import my.jbf.firstGame.libraries.DirectionInterface;


public class FightSurfaceview extends SurfaceView implements SurfaceHolder.Callback ,Runnable
{
	/*
	 **正文**
	 */
	public FightSurfaceview(Context context)
	{
		super(context);
		initEverything();

	}
	public FightSurfaceview(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initEverything();
	}
	private int TIME_IN_FRAME;
	private SharedPreferenceManager mSPM;
	private Player mPlayer;
	private boolean isAutoPlaying=false;
	private boolean isActivityShow;
	private SmallMap mSmallMap;
	private int BitmapW;
	private int BitmapH;
	private float scale;
	private Bitmap mBitmap;
	private FightMap mFightMap;
	private Enemy mEmeny;
	private Display dsp;
	private DirectionButton myDirectionButton;
	private AUTOButton myAUTOButton;
	private AttackButton myAttackButton;


	private void testshow()
	{

		/*
		 **新增测试刷新率代码**
		 */
		dsp = getDisplay();
		// dsp=getWindowManager().getDefaultDisplay();
		float rr=dsp.getRefreshRate();
		mCanvas.drawText(String.valueOf(rr), 50, 550, mPaint);
		//mCanvas.drawText(String.valueOf(mAttactKey.getButtonDownType()), 50, 100, mPaint);
		mCanvas.drawText(String.valueOf(mBuller.size()), 50, 150, mPaint);
		mCanvas.drawText((mSPM.getAttackButtonXY().x) + "," + (mSPM.getAttackButtonXY().y), 50, 200, mPaint);
		mSPM.resetWindowSize();
		mCanvas.drawText("DirectionButtonSize=" + (mSPM.getDirectionButtonSize()), 50, 300, mPaint);
		mCanvas.drawText(String.valueOf(mSPM.getGameFrame()), 50, 250, mPaint);
		mCanvas.drawText("windowW=" + (mSPM.getWindowSizeW()) + ",windowH=" + (mSPM.getWindowSizeH()), 50, 350, mPaint);
		mCanvas.drawText("AttactKey.xy=(" + (mSPM.getAttackButtonXY().x) + "," + (mSPM.getAttackButtonXY().y) + ")", 50, 400, mPaint);
		mCanvas.drawText("DirectionButton.xy=(" + (mSPM.getDirectionButtonXY().x) + "," + (mSPM.getDirectionButtonXY().y) + ")", 50, 450, mPaint);
		//mCanvas.drawBitmap(getFighttingBitmap(1000,800,0.5f),300,300,mPaint);
		mCanvas.drawText("move.xy=(" + myDirectionButton.getMoveContral().x + "," + myDirectionButton.getMoveContral().y + ")", 50, 500, mPaint);
	}
	public void setActivityShow()
	{
		isActivityShow = !isActivityShow;
	}

	public void setScale(float scale)
	{
		this.scale = scale;
	}
	public void setBitmapW(int bitmapW)
	{
		this.BitmapW = bitmapW;
	}

	public void setBitmapH(int bitmapH)
	{
		this.BitmapH = bitmapH;
	}



	//复制的代码
	/*屏幕刷新率设置*/


	//private player mplayer;
	private ArrayList<Bullet> mBuller;
	private GameDBManager mGameGameDBManager;
	/*屏幕刷新率设置完毕*/
	boolean playflag=true;
	/*左侧按钮设置*/


	/*屏幕刷新率设置完毕*/

	/*射击键设置*/


	/*射击键设置完毕*/

	/*小地图设置完毕*/

	/*自动游戏设置完毕*/

	private SurfaceHolder mSurfaceHolder;
	private Paint mPaint;
	private final boolean mIsALive=true;
	private Path mAutoPlayPath;
	private Path mNotAutoPlayPath;
	
	

	Canvas mCanvas;


	/*测试用数据设置*/
	String str;
	String str1;
	String str2;
	String str3;
	int pointnum=0;

	boolean ifdebug=true;
	/*测试用数据设置完毕*/

	/*测试用数据显示*/
	private void debugtest()
	{

		str1 = "point number:" + pointnum;
		mPaint.setColor(Color.WHITE);
		mPaint.setTextSize(40);



	}
	private void testinit()
	{


	}

	protected void initEverything()
	{

		mSPM = MainActivity.mSPM;
		isAutoPlaying = false;
		isActivityShow = true;
		TIME_IN_FRAME = mSPM.getGameFrame();


		mSmallMap = new SmallMap();
		mBuller = new ArrayList<Bullet>();


		setFocusable(true);
		setKeepScreenOn(true);
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		mCanvas = new Canvas();

		DisplayMetrics dm=new DisplayMetrics();
		((Activity)getContext()).getWindowManager().getDefaultDisplay().getMetrics(dm);
		int mScreenWidth = dm.widthPixels;
		int mScreenHeight = dm.heightPixels;


		mPaint = new Paint();
		mAutoPlayPath = new Path();
		mNotAutoPlayPath = new Path();
		mEmeny = new Enemy(new Point(500, 500));
		myDirectionButton = new DirectionButton();
		myAttackButton = new AttackButton();
		myAUTOButton = new AUTOButton();
		new Thread(myDirectionButton).start();
		new Thread(myAUTOButton).start();
		new Thread(myAttackButton).start();


		Path mAutoPlayButtonPath = new Path();

		PointF mAutoPlayButtonPointCenter = new PointF();



		int x,y;
		x = mScreenWidth;
		y = mScreenHeight;
		if (x < y)
		{
			int i = x;
			x = y;
			y = i;
		}


		int mAutoPlayButtonSize = x / 64;
		/*小地图设置*/




		mAutoPlayButtonPointCenter.set(x * 97 / 100 - mAutoPlayButtonSize, y / 2);


		mAutoPlayButtonPath.addCircle(mAutoPlayButtonPointCenter.x, mAutoPlayButtonPointCenter.y, mAutoPlayButtonSize, Path.Direction.CW);


		//画错的框在这里
		mAutoPlayPath.addRect(x / 3, y / 100, x * 99 / 100, y * 99 / 100, Path.Direction.CW);
		mNotAutoPlayPath.addRect(x / 100, y / 100, x * 99 / 100, y * 99 / 100 , Path.Direction.CW);
		//mplayer = new Player(mGameMap,true);
		mFightMap = new FightMap();


	}

	@Override
	public void surfaceCreated(SurfaceHolder p1)
	{


		Thread mThread = new Thread(this);

		mThread.start();
		// TODO: Implement this method
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder p1)
	{
		// TODO: Implement this method
	}

	@Override
	public void surfaceChanged(SurfaceHolder p1, int p2, int p3, int p4)
	{
		// TODO: Implement this method
	}

	@Override
	public void run()
	{
		while (playflag)
		{


			/**取得更新之前的时间**/
			long startTime = System.currentTimeMillis();

			/**在这里加上线程安全锁**/
			synchronized (mSurfaceHolder)
			{

				AI();
				render();

			}
			/**取得更新结束的时间**/
			long endTime = System.currentTimeMillis();

			/**计算出一次更新的毫秒数**/
			int diffTime  = (int)(endTime - startTime);


			/**确保每次更新时间为30帧**/
			while (diffTime <= 1000 / TIME_IN_FRAME) {
				diffTime = (int) (System.currentTimeMillis() - startTime);
				/**线程等待**/
				Thread.yield();
			}
			// TODO: Implement this method
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		// TODO: Implement this method
		//boolean mDirectionButtonDown;


		if (!isAutoPlaying && !isActivityShow)
		{
			//切换directionbutton
			myDirectionButton.setDirectionButtonTouchListener(event, new DirectionInterface() {
					@Override
					public void callback(PointF po)
					{
						mEmeny.setMove(po);
					}
				});
			//mDirectionButton.onTouchEvent(event);
			//mAttactKey.onTouchEvent(event);
		}




		switch (event.getActionMasked())
		{

			case MotionEvent.ACTION_DOWN:
				{
					if (event.getX() < 10200 && event.getY() < 600)
					{
//						playflag = !playflag;
//						Message msg=new Message();
//						msg.what = 2;
						//mhandler.sendEmptyMessage(2);

					}



					break;

				}
			case MotionEvent.ACTION_POINTER_DOWN:
				{




					break;
				}

			case MotionEvent.ACTION_MOVE:
				{


					break;
				}
			case MotionEvent.ACTION_POINTER_UP:
				{

					break;
				}
			case MotionEvent.ACTION_UP:
				{
					break;
				}
			case MotionEvent.ACTION_CANCEL:
				{

				}
		}

		return true;
	}


	private void drawInit()
	{

	}
	private void drawUI()
	{

		mCanvas.drawColor(mSPM.getBackGroundColor());
		mPaint.setColor(Color.BLACK);
		mPaint.setStyle(Paint.Style.STROKE);
		mCanvas.drawBitmap(mSmallMap.getBitmap(), mSPM.getSmallMapXY().x, mSPM.getSmallMapXY().y, mPaint);

		//mCanvas.drawBitmap(mFightMap.getmBitmap(),10,10,mPaint);
		if (!isAutoPlaying && !isActivityShow)
		{
			//切换新的directionbutton
			mCanvas.drawBitmap(myDirectionButton.getbitmap(), myDirectionButton.getLeftTopPoint().x, myDirectionButton.getLeftTopPoint().y, mPaint);
			mCanvas.drawBitmap(myAUTOButton.getbitmap(),mSPM.getAUTOButtonXY().x, mSPM.getAUTOButtonXY().y, mPaint);
			mCanvas.drawBitmap(myAttackButton.getbitmap(), mSPM.getAttackButtonXY().x, mSPM.getAttackButtonXY().y, mPaint);
		}
		//切换新的direcitonbutton


		mCanvas.drawBitmap(mEmeny.getbitmap(), mEmeny.getLeftTopPoint().x, mEmeny.getLeftTopPoint().y, mPaint);
		//mCanvas.drawBitmap(testpath(), 600, 600, mPaint);
//		if (mplayer.redent(mDirectionButton.getmContral(), mAttactKey.getButtonDownType()))
//		{
//			bullet temp=new bullet(mplayer.getScreenPointf());
//			mBuller.add(temp);
//			mAttactKey.setButtonDownType(0);
//		}
//		for (int i=mBuller.size() - 1;i >= 0;i--)
//		{
//			if (mBuller.get(i).redent())
//			{
//				mCanvas.drawBitmap(mBuller.get(i).getBitmap(), mBuller.get(i).getbulletCenterPointf().x, mBuller.get(i).getbulletCenterPointf().y, mPaint);
//
//			}
//			else mBuller.remove(i);
//		}
//
//		for (bullet number:mBuller)
//		{
//			if(number.redent())
//			{
//			mCanvas.drawBitmap(number.getBitmap(), number.getbulletCenterPointf().x, number.getbulletCenterPointf().y, mPaint);
//			}
//			else mBuller.remove(number);
//		}

		//mCanvas.drawBitmap(mplayer.getBitmap(), mplayer.getScreenPointf().x, mplayer.getScreenPointf().y, mPaint);
		/*自动游戏设置*/

		if (isAutoPlaying)
		{
			mCanvas.drawPath(mAutoPlayPath, mPaint);
		}
		else
		{
			mCanvas.drawPath(mNotAutoPlayPath, mPaint);

			//mCanvas.drawCircle(mRightButtonPointCenter.x, mRightButtonPointCenter.y, mRightButtonSize, mPaint);


			mPaint.setTextSize(30);

		}


		testshow();



	}
	private void render()
	{
		mCanvas = mSurfaceHolder.lockCanvas();
		//drawUI();
		try
		{
			if (mCanvas != null)
			{
				//mCanvas.save();
				drawInit();
				drawUI();
				mCanvas.drawBitmap(mEmeny.getbitmap(), mEmeny.getLeftTopPoint().x, mEmeny.getLeftTopPoint().y, mPaint);
				if (ifdebug)
				{
					debugtest();
				}
				boolean mGameIsContinue = true;
				if (mGameIsContinue)
				{
					if (true)
					{

					}
				}
			}

		}
		catch (Exception e)
		{

		}
		finally
		{
			if (mCanvas != null)
			{
				mSurfaceHolder.unlockCanvasAndPost(mCanvas);
			}
		}
	}
	private void AI()
	{

	}

	private Bitmap testpath()
	{
		Bitmap mb=Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
		Canvas mc=new Canvas(mb);
		Path p=new Path();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(1);

		mc.drawColor(Color.RED);
		mc.save();
		String pathsrt="M22,16V4c0,-1.1 -0.9,-2 -2,-2H8c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2zm-11,-4l2.03,2.71L16,11l4,5H8l3,-4zM2,6v14c0,1.1 0.9,2 2,2h14v-2H4V6H2z";

		MyParsePath pp=new MyParsePath();
		p.addPath(pp.parsePath(pathsrt));
		mc.scale(12, 12);
		mc.drawPath(p, mPaint);
		mc.restore();
		p.reset();
		String pathsr="A 0,0,100,100,0,180z";
		p.addPath(pp.parsePath(pathsr));
		mc.drawPath(p, mPaint);



		return mb;
	}
	public Bitmap getFighttingBitmap(int W, int H, float scale)
	{
		Bitmap mitmap=Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888);
		mCanvas.setBitmap(mitmap);
		scale = W / BitmapW;
		mCanvas.save();
		mCanvas.scale(0.5f, 0.5f);
		mCanvas.drawColor(Color.GREEN);

		//Canvas cc=new Canvas(mBitmap);

		//cc=mCanvas;
		mCanvas.restore();

		return mitmap;
	}








}
